# coding=utf-8

from biquge import BIQUGE
from web.models import *
import time
import urllib2

import thread

lock = thread.allocate_lock()
# import the logging library
import logging
logger = logging.getLogger('django')
biquge = BIQUGE()


def log_test():
    try:
        1/0
    except Exception as e:
        logger.debug(e)
        raise e


def update_page(page):

    #logger.debug(page.content)
    if page.content:
        logger.debug('book_id:%d page_id:%d had content' % (page.book_id, page.page_id))
        # if page.page_id == 15:
        #     logger.debug(page.content)
        return False

    try:
        logger.debug('crawlling content'+page.page_url)
        content = biquge.take_page(page.page_url)

    except Exception as e:
        print str(e)
        logger.debug(str(e)+str(page.page_url))
        return False

    page.content = content
    try:
        page.save()
        return True
    except Exception as e:
        print str(e)
        logger.debug(str(e)+str(page.page_url))
        return False


def update_pages(book_id):
    pages = Page.objects(book_id=book_id)
    for page in pages:
        update_page(page)


def update_book(p):
    book_id = p[0]
    v = p[1]
    k = 3
    print p
    logger.debug('gg')
    while k:
        try:
            book = biquge.take_book(book_id)
            k = 3
            logger.debug(book.get('book_title'))
            print 'hello'
            logger.debug(book_id)
            break
        except Exception as e:
            print str(e)
            logger.debug(str(e)+str(book_id))
            k -= 1
    if k != 3:
        logger.debug('%d crawl false!!!' % book_id)
        return False
    try:
        book_object = Book.objects.get(book_id=book_id)
    except DoesNotExist:
        book_object = Book(
            book_id=book.get('book_id'),
            book_title=book.get('book_title'),
            category=book.get('category'),
            author=book.get('author'),
            pic_url=book.get('pic_url')
        )

        try:
            book_object.save()
        except Exception as e:
            logger.debug(str(e)+str(book_id))
            return False

    for page_id, page in enumerate(book.get('page_urls')):
        try:
            page_object = Page.objects.get(page_url=page[1])
        except DoesNotExist:
            page_object = Page(
                page_id=page_id+1,
                book_id=book_object.book_id,
                book=book_object,
                page_title=page[0],
                page_url=page[1]
            )
            try:
                page_object.save()
            except Exception as e:
                print str(e)
                logger.debug(str(e)+str(book_id)+'//'+str(page_id))

        if v == 1 and page_object:
            update_page(page_object)






if __name__ == '__main__':
    connect('xiaoshuodb', username='xiaoshuoAdmin', password='xiaoshuo5741')
    update_book(1)
